package com.geen.tuoke.ui.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.geen.tuoke.base.BaseViewModel
import com.geen.tuoke.bean.CustomerInfo
import com.geen.tuoke.bean.Label
import com.geen.tuoke.bean.QueryCustomerModel
import com.geen.tuoke.network.api.apiService
import com.geen.tuoke.utils.ext.request
import com.geen.tuoke.utils.ext.toastShort

/**
 * @Desc  客户相关
 */
class CustomerViewModel : BaseViewModel() {


    var addCustomerResult = MutableLiveData<Boolean>()

    var customerList = MutableLiveData<List<CustomerInfo>>()
    var labelList = MutableLiveData<List<Label>>()
    var addLabelResult = MutableLiveData<Label>()

    var pageNo : Int = 1
    var pageSize :Int = 20
    var totalPage : Int = 0

    //添加客户
    fun addCustomer(customerInfo: CustomerInfo) {

        request({ apiService.addCustomer(customerInfo) }, {
            addCustomerResult.value = true
        }, {
            addCustomerResult.value = false
            toastShort(it.errorMsg)
        }, true)

    }

    //查询客户
    fun getCustomerList(showLoading:Boolean = true) {
        val query = QueryCustomerModel(-1, "")
        request({ apiService.getCustomerList(query,pageNo,pageSize) }, {
            totalPage = it.totalPage
            customerList.value = it.records
        }, {

            toastShort(it.errorMsg)
        }, showLoading)
    }

    //添加标签
    fun updateCustomer(customerInfo: CustomerInfo) {
        request({ apiService.updateCustomer(customerInfo) }, {

        }, {
            toastShort(it.errorMsg)
        }, true)
    }
    //添加标签
    fun addLabel(name: String) {
        request({ apiService.addLabel(name) }, {
            addLabelResult.value=it
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    //获取标签
    fun getLabelList() {
        request({ apiService.getLabelList() }, {
            labelList.value = it
        }, {
            toastShort(it.errorMsg)
        }, true)
    }


    interface Handler {

    }
}
